/**
 *
 */
let vm = new Vue({
    el:'#vm',
    data(){
        let validateOldPass = (rule, value, callback) => {
            if (value === '') {
                return callback(new Error('请输入密码'));
            }
            callback();
        };
        let validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入新密码'));
            } else {
                if (this.ruleForm.newPasswordRepeat !== '') {
                    this.$refs.ruleForm.validateField('newPasswordRepeat');
                }
                callback();
            }
        };
        let validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.ruleForm.newPassword) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };

        return {
            dictList:undefined,
            menu:{},
            userInfo:'',
            dialogFormVisible:false,
            theme:false,
            ruleForm: {
                password: undefined,
                newPassword: undefined,
                newPasswordRepeat: undefined
            },
            rules: {
                password: [ { validator: validateOldPass, trigger: 'blur' }],
                newPassword: [{ validator: validatePass, trigger: 'blur' }],
                newPasswordRepeat: [{ validator: validatePass2, trigger: 'blur' }]
            }
        };

    },
    created:function (){
        this.getMenu();
    },
    methods:{
        getMenu(){
            $.ajax({
                url:'/sys/menu/getMenuByUserId',
                type:'get',
                dataType:'json',
                data:{},
                success:(res)=>{
                    this.menu = res.data;
                }
            })
        },
        openDialog(){
            this.dialogFormVisible=true;
        },
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    axios.put('/sys/user/modifyPwd',this.ruleForm)
                    .then(res => {
                        this.$notify({
                            title: '提示',
                            message: res.data.msg,
                            type: 'success'
                        });
                    }).catch(()=>{
                        this.$notify.error({
                            title: '提示',
                            message: '修改失败'
                        });
                    })
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        switchTheme(){
            console.log(this.theme)
            if (this.theme){
                var link = document.createElement('link');
                link.type = 'text/css';
                link.id = "theme-css-dark";  // 加上id方便后面好查找到进行删除
                link.rel = 'stylesheet';
                link.href = '/assets/css/admin-default.css';
                document.getElementsByTagName("head")[0].appendChild(link);
            } else {
                $('#theme-css-dark').remove();
            }
        }
    }
})
